/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
 * Nihongo
 * Copyright (C) Massimiliano Maniscalco 2011, 2012 <massi.neptune@yahoo.com>
 * 
Nihongo is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Nihongo is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "test_inserimento_parole.h"

void TestInserimentoParole::
run_test ( char *options )
{
	int nr = _contenuto->size();
	_domandeTotLabel->set_text ( Glib::ustring::format ( nr ) );
	_testWin->present ();
	_timer = 0;
	_nrDom = 0;
	_modo = options[0];
	_verso = options[1];
	update_window ();
}

void TestInserimentoParole::
update_window ( void )
{
	Glib::ustring _domanda;
	_domandaNumLabel->set_text ( Glib::ustring::format ( _nrDom + 1 ) );
	_timerConnection = Glib::signal_timeout ().connect_seconds (sigc::mem_fun (this, &TestInserimentoParole::on_timer_fired), 1 );
	_tempoLabel->set_text ( "00 : 00" );
	_domanda = _parolaCorrente->get_kotoba ();
	_domandaLabel->set_text ( _domanda );
	_rispostaEntry->set_text ( "" );
}

void TestInserimentoParole::
on_risposta ( char tasto )
{
	Glib::ustring risposta = _rispostaEntry->get_text ();
	bool risultato = ( risposta.raw() == _parolaCorrente->get_yomikata ().raw() );
	_timerConnection.disconnect ();
	_parolaCorrente->test ( _modo, risultato, _timer );
	commit_parole_changes.emit ( *_parolaCorrente );
	if ( !risultato )
	{
		Glib::ustring messaggio = "Sbagliato, la risposta giusta è\n<b><big>" + _parolaCorrente->get_yomikata () + "</big></b>";
		Gtk::MessageDialog *_alertDialog = new Gtk::MessageDialog ( *_testWin, "", false, Gtk::MESSAGE_WARNING, Gtk::BUTTONS_OK, true);
		_alertDialog->set_message ( messaggio, true);
		int res_id = _alertDialog->run();
		delete _alertDialog;
	}
	next_question ();
}

void TestInserimentoParole::
next_question ( void )
{
	_timer = 0;
	_nrDom++;
	_parolaCorrente++;
	if ( _parolaCorrente == _contenuto->end() )
		_testWin->hide();
	else
		update_window ();
}

bool TestInserimentoParole::
show_result ( void )
{
	bool presented;
	_resultCtrl->set_modo ( _modo );
	presented = _resultCtrl->present_window ();
	return presented;
}
